package com.serviceprovider.business.utility;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.utility.maintenance.settings.RoleUtils;

import org.springframework.security.GrantedAuthority;
import org.springframework.security.context.SecurityContextHolder;

public class SecurityContextUser {
    public static boolean isAdmin() {
        return hasRole(RoleUtils.ROLE_ADMINISTRATOR);
    }

    public static boolean isServiceProvider() {
        return hasRole(RoleUtils.ROLE_SERVICE_PROVIDER);
    }

    public static boolean isPayee() {
        return hasRole(RoleUtils.ROLE_PAYEE);
    }

    public static String getUserName() {
        return SecurityContextHolder.getContext().getAuthentication().getName();
    }

    private static boolean hasRole(String role) {
        boolean            result      = false;
        GrantedAuthority[] authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();

        for (int i = 0; (i < authorities.length) &&!result; i++) {
            GrantedAuthority authority = authorities[i];

            result = authority.getAuthority().equals(role);
        }

        return result;
    }
}



